<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="公告名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入公告名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目名称" prop="procurementMethod">
        <el-input
          v-model="queryParams.procurementMethod"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="包租名称" prop="packageGroupName">
        <el-input
          v-model="queryParams.packageGroupName"
          placeholder="请输入包租名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="包租预算价" prop="packageGroupBudgetPrice">
        <el-input
          v-model="queryParams.packageGroupBudgetPrice"
          placeholder="请输入包租预算价"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核员审批(0:未审批,1:已审批)" prop="reviewerApproval">
        <el-input
          v-model="queryParams.reviewerApproval"
          placeholder="请输入审核员审批(0:未审批,1:已审批)"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总经理审批(0:未审批,1:已审批)" prop="managerApproval">
        <el-input
          v-model="queryParams.managerApproval"
          placeholder="请输入总经理审批(0:未审批,1:已审批)"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['announce:announce:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['announce:announce:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['announce:announce:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['announce:announce:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="announceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="中标公告ID" align="center" prop="id" />
      <el-table-column label="公告编号" align="center" prop="announcementNo" />
      <el-table-column label="公告名称" align="center" prop="title" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="项目编号" align="center" prop="projectCode" />
      <el-table-column label="项目名称" align="center" prop="procurementMethod" />
      <el-table-column label="包租编号" align="center" prop="packageGroupId" />
      <el-table-column label="包租名称" align="center" prop="packageGroupName" />
      <el-table-column label="包租预算价" align="center" prop="packageGroupBudgetPrice" />
      <el-table-column label="说明" align="center" prop="description" />



        <el-table-column label="公告内容" align="center" prop="content" />
        <el-table-column label="附件" align="center" prop="attachment" />
        <el-table-column label="审核员审批(0:未审批,1:已审批)" align="center" prop="reviewerApproval" />
        <el-table-column label="总经理审批(0:未审批,1:已审批)" align="center" prop="managerApproval" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">

        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['announce:announce:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['announce:announce:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>




    <el-table v-loading="loading" :data="personList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="中标公告ID" align="center" prop="id" />-->
      <el-table-column label="类型" align="center" prop="type" />
      <el-table-column label="公司名字" align="center" prop="name" />
      <el-table-column label="排名" align="center" prop="ranking" />
      <el-table-column label="最终报价" align="center" prop="finalPrice" />
      <el-table-column label="项目名称" align="center" prop="score" />
      <el-table-column label="联系电话" align="center" prop="phone" />
<!--      <el-table-column label="包租名称" align="center" prop="packageGroupName" />-->
<!--      <el-table-column label="包租预算价" align="center" prop="packageGroupBudgetPrice" />-->
<!--      <el-table-column label="说明" align="center" prop="description" />-->

      <!--      <el-table-column label="公告内容" align="center" prop="content" />-->
      <!--      <el-table-column label="附件" align="center" prop="attachment" />-->
      <!--      <el-table-column label="审核员审批(0:未审批,1:已审批)" align="center" prop="reviewerApproval" />-->
      <!--      <el-table-column label="总经理审批(0:未审批,1:已审批)" align="center" prop="managerApproval" />-->
      <!--      <el-table-column label="备注" align="center" prop="remark" />-->
      <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->


<!--      <el-table-column label="公告内容" align="center" prop="content" />-->
<!--      <el-table-column label="附件" align="center" prop="attachment" />-->
<!--      <el-table-column label="审核员审批(0:未审批,1:已审批)" align="center" prop="reviewerApproval" />-->
<!--      <el-table-column label="总经理审批(0:未审批,1:已审批)" align="center" prop="managerApproval" />-->
      <el-table-column label="备注" align="center" prop="remark" />
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->

<!--        <template #default="scope">-->
<!--          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['announce:announce:edit']">修改</el-button>-->
<!--          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['announce:announce:remove']">删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公告管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="announceRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="公告编号" prop="announcementNo">
          <el-input v-model="form.announcementNo" placeholder="请输入公告编号" />
        </el-form-item>
        <el-form-item label="公告名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入公告名称" />
        </el-form-item>
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="form.projectName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="项目编号" prop="projectCode">
          <el-input v-model="form.projectCode" placeholder="请输入项目编号" />
        </el-form-item>
        <el-form-item label="项目名称" prop="procurementMethod">
          <el-input v-model="form.procurementMethod" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="包租编号" prop="packageGroupId">
          <el-input v-model="form.packageGroupId" placeholder="请输入包租编号" />
        </el-form-item>
        <el-form-item label="包租名称" prop="packageGroupName">
          <el-input v-model="form.packageGroupName" placeholder="请输入包租名称" />
        </el-form-item>
        <el-form-item label="包租预算价" prop="packageGroupBudgetPrice">
          <el-input v-model="form.packageGroupBudgetPrice" placeholder="请输入包租预算价" />
        </el-form-item>
        <el-form-item label="说明" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="公告内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="附件" prop="attachment">
          <el-input v-model="form.attachment" placeholder="请输入附件" />
        </el-form-item>
        <el-form-item label="审核员审批(0:未审批,1:已审批)" prop="reviewerApproval">
          <el-input v-model="form.reviewerApproval" placeholder="请输入审核员审批(0:未审批,1:已审批)" />
        </el-form-item>
        <el-form-item label="总经理审批(0:未审批,1:已审批)" prop="managerApproval">
          <el-input v-model="form.managerApproval" placeholder="请输入总经理审批(0:未审批,1:已审批)" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-divider content-position="center">人员管理信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" @click="handleAddPerson">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="Delete" @click="handleDeletePerson">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="personList" :row-class-name="rowPersonIndex" @selection-change="handlePersonSelectionChange" ref="person">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="中标人/候选人" prop="type" width="150">
<!--            <template #default="scope">-->
<!--              <el-select v-model="scope.row.type" placeholder="请选择中标人/候选人">-->
<!--                <el-option label="请选择字典生成" value="" />-->
<!--              </el-select>-->
<!--            </template>-->
            <template #default="scope">
              <el-input v-model="scope.row.type" placeholder="中标/候选" />
            </template>
          </el-table-column>
          <el-table-column label="公司名字" prop="name" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.name" placeholder="请输入公司名字" />
            </template>
          </el-table-column>
          <el-table-column label="排名" prop="ranking" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.ranking" placeholder="请输入排名" />
            </template>
          </el-table-column>
          <el-table-column label="最终报价" prop="finalPrice" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.finalPrice" placeholder="请输入最终报价" />
            </template>
          </el-table-column>
          <el-table-column label="得分" prop="score" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.score" placeholder="请输入得分" />
            </template>
          </el-table-column>
          <el-table-column label="联系电话" prop="phone" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.phone" placeholder="请输入联系电话" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Announce">
import { listAnnounce, getAnnounce, delAnnounce, addAnnounce, updateAnnounce } from "@/api/announce/announce"

const { proxy } = getCurrentInstance()

const announceList = ref([])
const personList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const checkedPerson = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    title: null,
    projectName: null,
    procurementMethod: null,
    packageGroupName: null,
    packageGroupBudgetPrice: null,
    reviewerApproval: null,
    managerApproval: null,
  },
  rules: {
    announcementNo: [
      { required: true, message: "公告编号不能为空", trigger: "blur" }
    ],
    title: [
      { required: true, message: "公告名称不能为空", trigger: "blur" }
    ],
    projectName: [
      { required: true, message: "项目名称不能为空", trigger: "blur" }
    ],
    projectCode: [
      { required: true, message: "项目编号不能为空", trigger: "blur" }
    ],
    packageGroupBudgetPrice: [
      { required: true, message: "包租预算价不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询公告管理列表 */
function getList() {
  loading.value = true
  listAnnounce(queryParams.value).then(response => {
    announceList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    announcementNo: null,
    title: null,
    projectName: null,
    projectCode: null,
    procurementMethod: null,
    packageGroupId: null,
    packageGroupName: null,
    packageGroupBudgetPrice: null,
    description: null,
    content: null,
    attachment: null,
    reviewerApproval: null,
    managerApproval: null,
    delFlag: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  }
  personList.value = []
  proxy.resetForm("announceRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加公告管理"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getAnnounce(_id).then(response => {
    form.value = response.data
    personList.value = response.data.personList
    open.value = true
    title.value = "修改公告管理"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["announceRef"].validate(valid => {
    if (valid) {
      form.value.personList = personList.value
      if (form.value.id != null) {
        updateAnnounce(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addAnnounce(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除公告管理编号为"' + _ids + '"的数据项？').then(function() {
    return delAnnounce(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 人员管理序号 */
function rowPersonIndex({ row, rowIndex }) {
  row.index = rowIndex + 1
}

/** 人员管理添加按钮操作 */
function handleAddPerson() {
  let obj = {}
  obj.type = ""
  obj.name = ""
  obj.ranking = ""
  obj.finalPrice = ""
  obj.score = ""
  obj.phone = ""
  obj.remark = ""
  personList.value.push(obj)
}

/** 人员管理删除按钮操作 */
function handleDeletePerson() {
  if (checkedPerson.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的人员管理数据")
  } else {
    const persons = personList.value
    const checkedPersons = checkedPerson.value
    personList.value = persons.filter(function(item) {
      return checkedPersons.indexOf(item.index) == -1
    })
  }
}

/** 复选框选中数据 */
function handlePersonSelectionChange(selection) {
  checkedPerson.value = selection.map(item => item.index)
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('announce/announce/export', {
    ...queryParams.value
  }, `announce_${new Date().getTime()}.xlsx`)
}

getList()
</script>
